package at.btd.bsk.entities;

import java.util.List;
import java.util.Vector;

import at.btd.bsk.util.Date;

public class Trip {
	private static final long serialVersionUID = 3050093983774457949L;

	/** date of the run */
	private Date date = null;
	/** total time of the run in seconds */
	private long timeInSeconds = 0L;
	/** total distance of the run */
	private float distanceInKm = 0.f;
	/** average speed of the run */
	private float speed = 0.f;
	/** all waypoints of the run */
	private List<Waypoint> waypoints = null;

	public Trip(Date date, long timeInSeconds, float distanceInKm) {
		this.date = date;
		this.timeInSeconds = timeInSeconds;
		this.distanceInKm = distanceInKm;

		this.waypoints = new Vector<Waypoint>();

		updateSpeed();
	}

	public Date getDate() {
		return this.date;
	}

	public void setDate(Date date) {
		this.date = date;
	}

	public long getTime() {
		return this.timeInSeconds;
	}

	public void setTimeInSeconds(long time) {
		this.timeInSeconds = time;
	}

	public String getTimeString() {
		int seconds = (int) (this.timeInSeconds % 60);
		int minutes = (int) (this.timeInSeconds / 60) % 60;
		int hours = (int) (this.timeInSeconds / 3600);

		String minutesString = minutes < 10 ? "0" + minutes : String.valueOf(minutes);
		String secondsString = seconds < 10 ? "0" + seconds : String.valueOf(seconds);

		return hours + ":" + minutesString + ":" + secondsString;
	}

	public float getDistanceInKm() {
		return this.distanceInKm;
	}

	public void setDistanceInKm(float distance) {
		this.distanceInKm = distance;
	}

	public float getSpeed() {
		return this.speed;
	}

	public void setSpeed(float speed) {
		this.speed = speed;
	}

	public void addWaypoint(Waypoint p) {
		this.waypoints.add(p);
	}

	public List<Waypoint> getWaypoints() {
		return this.waypoints;
	}
	
	public void setWaypoints(List<Waypoint> waypoints) {
		this.waypoints = waypoints;
	}

	@Override
	public String toString() {
		return this.date.toFullString() + ", " + this.getTimeString() + ", " + this.getSpeed();
	}

	private void updateSpeed() {
		if (this.timeInSeconds / 3600.f > 0)
			this.speed = this.distanceInKm / (this.timeInSeconds / 3600.f);
	}
}
